import http from "@/axios/index.js";
import { handleError, handleSuccess } from "@/utils/responseHandler.js";

export const MenuApi = {
    // 1. 创建新菜单
    createMenu: (menuData) => {
        return http.post('/menu/create', menuData)
            .then(res => handleSuccess(res, '创建菜单'))
            .catch(error => handleError(error, '创建菜单', '创建菜单失败'));
    },

    // 2. 更新菜单
    updateMenu: (menuData) => {
        return http.put('/menu/update', menuData)
            .then(res => handleSuccess(res, '更新菜单'))
            .catch(error => handleError(error, '更新菜单', '更新菜单失败'));
    },

    // 3. 删除菜单
    deleteMenu: (menuId) => {
        return http.delete(`/menu/delete/${menuId}`)
            .then(res => handleSuccess(res, '删除菜单'))
            .catch(error => handleError(error, '删除菜单', '删除菜单失败'));
    },

    // 4. 菜单分页查询
    getMenuList: (params) => {
        return http.get('/menu/list', { params })
            .then(res => handleSuccess(res, '获取菜单列表'))
            .catch(error => handleError(error, '获取菜单列表', '获取菜单列表失败'));
    },

    // 5. 获取菜单详情
    getMenuDetail: (menuId) => {
        return http.get(`/menu/detail/${menuId}`)
            .then(res => handleSuccess(res, '获取菜单详情'))
            .catch(error => handleError(error, '获取菜单详情', '获取菜单详情失败'));
    },

    // 6. 获取菜单树
    getMenuTree: () => {
        return http.get('/menu/tree')
            .then(res => handleSuccess(res, '获取菜单树'))
            .catch(error => handleError(error, '获取菜单树', '获取菜单树失败'));
    },

    // 7. 更新菜单排序和父级
    updateMenuTree: (treeData) => {
        return http.put('/menu/updatetree', treeData)
            .then(res => handleSuccess(res, '更新菜单结构'))
            .catch(error => handleError(error, '更新菜单结构', '更新菜单结构失败'));
    }
}
